package br.ucb.cesbdao.visao;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;

import org.apache.log4j.Logger;

import br.ucb.cesbdao.servicos.Servicos;
import br.ucb.cesbdao.utilBeans.TrabalhadorUtil;

public class AlteraSenha extends JDialog {
	private static final long serialVersionUID = 2048520014901390915L;
	
	
	private final JPanel contentPanel = new JPanel();
	private JPasswordField txtOldSenha;
	private JPasswordField txtNewSenha;
	private JPasswordField txtConSenha;
	private JLabel lblforcaSenha;
	private JLabel lblErroOldSenha;
	private JLabel lblErroConSenha;
	static Logger logger = Servicos.carregaLog(AlteraSenha.class);

	/**
	 * Create the dialog.
	 */
	public AlteraSenha() {
		setTitle("Alterar Senha");
		setModal(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 504, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JLabel lblSenhaAntiga = new JLabel("Senha Antiga:");
		lblSenhaAntiga.setBounds(11, 37, 76, 16);
		
		txtOldSenha = new JPasswordField();
		txtOldSenha.setEchoChar('●');
		txtOldSenha.setBounds(113, 31, 203, 28);
		
		JLabel lblSenhaNova = new JLabel("Senha Nova:");
		lblSenhaNova.setBounds(11, 83, 70, 16);
		
		txtNewSenha = new JPasswordField();
		txtNewSenha.setEchoChar('●');
		txtNewSenha.addKeyListener(new KeyAdapter() {
			@SuppressWarnings("deprecation")
			@Override
			public void keyReleased(KeyEvent arg0) {
				Servicos.CheckPasswordStrength(txtNewSenha.getText(),lblforcaSenha);
			}
		});
		txtNewSenha.setBounds(113, 77, 203, 28);
		txtNewSenha.setColumns(10);
		
		JLabel lblConfirmaSenha = new JLabel("Confirma Senha:");
		lblConfirmaSenha.setBounds(11, 129, 93, 16);
		
		txtConSenha = new JPasswordField();
		txtConSenha.setEchoChar('●');
		txtConSenha.setBounds(113, 129, 203, 28);
		txtConSenha.setColumns(10);
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(TrabalhadorUtil.alteraSenha(txtOldSenha,txtNewSenha,txtConSenha,lblErroOldSenha,lblforcaSenha,lblErroConSenha))
					dispose();
			}
		});
		btnOk.setBounds(129, 195, 114, 44);
		btnOk.setIcon(new ImageIcon(AlteraSenha.class.getResource("/br/ucb/cesbdao/imagens/iconSalvar.png")));
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnCancelar.setBounds(261, 195, 114, 44);
		btnCancelar.setIcon(new ImageIcon(AlteraSenha.class.getResource("/br/ucb/cesbdao/imagens/iconCancelar.png")));
		contentPanel.setLayout(null);
		contentPanel.add(btnOk);
		contentPanel.add(btnCancelar);
		contentPanel.add(lblSenhaAntiga);
		contentPanel.add(lblSenhaNova);
		contentPanel.add(txtNewSenha);
		contentPanel.add(txtOldSenha);
		contentPanel.add(lblConfirmaSenha);
		contentPanel.add(txtConSenha);
		
		lblforcaSenha = new JLabel("");
		lblforcaSenha.setBackground(Color.WHITE);
		lblforcaSenha.setFont(new Font("Times New Roman", Font.BOLD, 13));
		lblforcaSenha.setBounds(330, 83, 152, 16);
		contentPanel.add(lblforcaSenha);
		
		lblErroConSenha = new JLabel("");
		lblErroConSenha.setFont(new Font("Times New Roman", Font.BOLD, 13));
		lblErroConSenha.setBackground(Color.WHITE);
		lblErroConSenha.setBounds(330, 135, 152, 16);
		contentPanel.add(lblErroConSenha);
		
		lblErroOldSenha = new JLabel("");
		lblErroOldSenha.setFont(new Font("Times New Roman", Font.BOLD, 13));
		lblErroOldSenha.setBackground(Color.WHITE);
		lblErroOldSenha.setBounds(330, 37, 152, 16);
		contentPanel.add(lblErroOldSenha);
	}
}
